package com.hm.oauth.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Component;

import com.hm.common.common.BeanUtil;
import com.hm.common.common.CommonUtil;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.model.PageInfo;
import com.hm.oauth.model.ClientModel;
import com.hm.oauth.model.QClientModel;
import com.hm.oauth.repository.ClinetRepository;
import com.hm.oauth.vo.ClientVo;
import com.mysema.query.types.expr.BooleanExpression;

/**
 * @author shishun.wang
 * @date 下午5:35:45 2016年8月30日
 * @version 1.0
 * @describe
 */
@Component
public class ClientService {

	private static final Logger logger = LoggerFactory.getLogger(ClientService.class);

	@Autowired
	private ClinetRepository clinetRepository;

	public String add(ClientVo clientVo) {
		Iterator<ClientModel> iterator = clinetRepository.findAll(QClientModel.clientModel.name.eq(clientVo.getName()))
				.iterator();
		while (iterator.hasNext()) {
			throw ServiceException.warn("该客户端名称已存在,请重新选择");
		}

		ClientModel clientModel = (ClientModel) BeanUtil.copyProperties(new ClientModel(), clientVo);
		clientModel.setClientSecret(CommonUtil.generationToken());
		clientModel.setClientId(CommonUtil.generationToken());

		return clinetRepository.save(clientModel).getId();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageInfo<ClientVo> query(int pageNo, int pageSize) {
		PageInfo<ClientVo> pageInfo = new PageInfo<ClientVo>(pageNo, pageSize);

		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				QClientModel.clientModel.createTime.desc());
		BooleanExpression predicate = QClientModel.clientModel.state.eq(1);

		List<ClientVo> vos = new ArrayList<ClientVo>();
		Page page = clinetRepository.findAll(predicate, pageable);
		page.getContent().forEach(clientModel -> {
			vos.add((ClientVo) BeanUtil.copyProperties(new ClientVo(), clientModel));
		});

		return pageInfo.execute(page, vos);
	}

	public ClientVo get(String id) {
		ClientModel clientModel = clinetRepository.findOne(id);
		if (clientModel == null) {
			logger.debug("don't find {} clint info", id);
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, id);
		}
		return (ClientVo) BeanUtil.copyProperties(new ClientVo(), clientModel);
	}
	
	public ClientVo getClinetByClientId(String clientId) {
		ClientModel clientModel = clinetRepository.findOne(QClientModel.clientModel.clientId.eq(clientId));
		if (clientModel == null) {
			logger.debug("don't find {} clint info ,clientId is ", clientId);
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, clientId);
		}
		return (ClientVo) BeanUtil.copyProperties(new ClientVo(), clientModel);
	}

	public String delete(String id) {
		ClientModel clientModel = clinetRepository.findOne(id);
		if (clientModel == null) {
			logger.debug("don't find {} clint info", id);
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, id);
		}
		clientModel.setState(0);
		return clinetRepository.save(clientModel).getId();
	}
}
